package com.example.bishe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bishe.entity.Message;
import com.example.bishe.mapper.MessageMapper;
import com.example.bishe.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LDH
 * @since 2023/04/02
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    /**
    * 功能描述: 用户查询消息通知
    * @Param: [pageNum, pageSize, userId]
    * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.example.bishe.entity.Message>
    * @Author: LDH
    * @Date: 2023/04/23
    */
    @Override
    public IPage<Message> getMessage(int pageNum, int pageSize, Long userId) {
        IPage<Message> page = new Page<>(pageNum, pageSize);
        return baseMapper.getMessage(page, userId);
    }

    /**
    * 功能描述: 管理员查询消息通知
    * @Param: [pageNum, pageSize, id, uid, context, type]
    * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.example.bishe.entity.Message>
    * @Author: LDH
    * @Date: 2023/05/08
    */
    @Override
    public IPage<Message> getPage(int pageNum, int pageSize, Long id, Long uid, String context, Integer type, String startTime, String endTime) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        if (id != null)
            queryWrapper.like(Message::getId, id);
        if (uid != null)
            queryWrapper.like(Message::getUid, uid);
        if (StringUtils.isNotBlank(context))
            queryWrapper.like(Message::getContext, context);
        if (type != null)
            queryWrapper.eq(Message::getType, type);
        if (StringUtils.isNotBlank(startTime))
            queryWrapper.ge(Message::getCreateTime,startTime);
        if (StringUtils.isNotBlank(endTime))
            queryWrapper.le(Message::getUpdateTime, endTime);
        queryWrapper.orderByDesc(Message::getCreateTime);
        IPage<Message> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
    * 功能描述: 管理员新建消息通知
    * @Param: [message]
    * @return: void
    * @Author: LDH
    * @Date: 2023/05/08
    */
    @Override
    public void add(Message message) {
        baseMapper.insert(message);
    }

    /**
    * 功能描述: 管理员删除单个消息
    * @Param: [message]
    * @return: void
    * @Author: LDH
    * @Date: 2023/05/08
    */
    @Override
    public void delOne(Message message) {
        baseMapper.deleteById(message);
    }

    /**
    * 功能描述: 管理员批量删除消息通知
    * @Param: [messages]
    * @return: void
    * @Author: LDH
    * @Date: 2023/05/08
    */
    @Override
    public void delBatch(List<Message> messages) {
        List<Long> list = messages.stream().map(Message::getId).collect(Collectors.toList());
        baseMapper.deleteBatchIds(list);
    }
}
